<template>
  <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :visible.sync="dialogFormVisible"
    :before-close="close"
    :title="title"
    width="800px"
    :top="'10vh'"
  >
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="布局名称" prop="name" style="width: 100%">
            <el-input v-model="form.name" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开放对象" prop="kfdx" style="width: 100%">
            <el-radio-group v-model="form.kfdx">
              <el-radio
                v-for="item in dict.service_dx"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="字体颜色" prop="defColor">
            <el-color-picker v-model="form.defColor" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="背景颜色" prop="backgroundColor">
            <el-color-picker v-model="form.backgroundColor" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="sfqy">
            <el-radio-group v-model="form.sfqy">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { saveObject, updateObject } from '@/api/mobile/layout/index'
export default {
  name: 'MobileLayoutEntity',
  dicts: ['service_dx'],
  data() {
    return {
      loading: false,
      dialogFormVisible: false,
      title: '新增移动门户布局',
      status: [{ id: 0, value: false, label: '否' },
        { id: 1, value: true, label: '是' }],
      form: {
        id: null,
        name: '',
        kfdx: '',
        backgroundColor: '',
        defColor: '',
        sfmr: true,
        sfqy: true,
        sfsc: false
      },
      rules: {
        name: [{ required: true, message: '请输入布局名称', trigger: 'blur' }]
      }
    }
  },
  methods: {
    openEntity(data) {
      if (undefined !== data && undefined !== data.id && data.id !== null) {
        this.form = Object.assign({}, data)
        this.title = '修改移动门户布局'
        this.dialogFormVisible = true
      } else {
        this.title = '新增移动门户布局'
        this.dialogFormVisible = true
      }
    },
    save(formName) {
      this.loading = true
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.form.id !== null && undefined !== this.form.id) {
            updateObject(this.form).then(res => {
              this.$message.success('更新成功！')
              this.close()
            }).catch(() => {
              this.loading = false
              this.$message.error('更新失败！')
            })
          } else {
            saveObject(this.form).then(res => {
              this.$message.success('保存成功！')
              this.close()
            }).catch(() => {
              this.loading = false
              this.$message.error('保存失败！')
            })
          }
        } else {
          this.loading = false
          console.log('error submit!!')
          return false
        }
      })
    },
    close() {
      this.loading = false
      Object.assign(this.$data.form, this.$options.data.call(this).form)
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    }
  }
}
</script>

<style scoped>

</style>
